package com.kingtone.rongji;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.kingtone.rongji.biz.CommonMessage;
import com.kingtone.rongji.network.UserRequest;
import com.kingtone.rongji.ui.ToastManager;

public class PasswordStep22Activity extends BaseActivity {

	private static final String TAG = "PasswordStep22Activity";

	private Activity mContext;
	
	private Button mNextBtn;

	private EditText mPhone;
	
	private String token;
	
	private int userId;
	
	private boolean needHandleResult = false;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.mContext = this;
		this.setContentView(R.layout.activity_find_pwd_step22);
		setupView();
	}

	private void setupView() {
		initTitleView(getString(R.string.title_find_pwd));
		
		token = getIntent().getExtras().getString("Token");
		String phone = getIntent().getExtras().getString("CellPhone");
		userId = getIntent().getExtras().getInt("UserId");
		
		mPhone = (EditText) this.findViewById(R.id.id_et_phone);
		mPhone.setText(phone);

		Button verifyCode = (Button) this.findViewById(R.id.id_btn_verifyCode);
		verifyCode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String phone = mPhone.getText().toString();
				if (phone.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.login_phone_empty_label));
					return;
				} 
				needHandleResult = false;
				UserRequest userRequest = new UserRequest();
				userRequest.getVerifyCode(userId, phone, PasswordStep22Activity.this);
			}
		});
		
		final EditText verifyCodeET = (EditText) this.findViewById(R.id.id_et_verifyCode);
		
		mNextBtn = (Button) this.findViewById(R.id.id_bt_next);
		mNextBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String phone = mPhone.getText().toString();
				if (phone.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.login_phone_empty_label));
					return;
				}
				String smsCode = verifyCodeET.getText().toString();
				if (smsCode.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.login_verifyCode_empty_label));
					return;
				} 

				needHandleResult = true;
				UserRequest userRequest = new UserRequest();
				userRequest.checkVerifyCode(userId, smsCode, PasswordStep22Activity.this);
			}
		});
	}
	
	@Override
	public void onBussinessSuccess(Object obj) {
		// TODO Auto-generated method stub
		super.onBussinessSuccess(obj);
		if(obj instanceof CommonMessage)
		{
			if(needHandleResult)
			{
				Intent intent = new Intent(PasswordStep22Activity.this, PasswordActivity.class);
				intent.putExtra("ResetPassword", true);
				intent.putExtra("PublicToken", token);
				intent.putExtra("UserId", String.valueOf(userId));
				startActivity(intent);
			}
		}
	}
}
